package com.dq.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dq.commonutils.pojo.DqException;
import com.dq.commonutils.pojo.ResultCode;
import com.dq.commonutils.vo.Result;
import com.dq.eduservice.client.VodClient;
import com.dq.eduservice.entity.EduVideo;
import com.dq.eduservice.service.EduVideoService;
import com.dq.eduservice.mapper.EduVideoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dq.eduservice.vo.chapter.VideoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程视频(EduVideo)表服务实现类
 *
 * @author Dq
 * @since 2022-07-22 19:51:20
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    private EduVideoMapper videoMapper;


    @Autowired
    private VodClient vodClient;

    @Override
    public List<EduVideo> getEduVideoByChapterId(String chapterId) {
        /**
         * 1. 根据id当前的课程（course）查询一个（或多个）小节 然后返回
         * 2. 我采取一个小节
         * 4. 返回结果集
         */
        LambdaQueryWrapper<EduVideo> eduVideoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduVideoLambdaQueryWrapper.eq(EduVideo::getChapterId, chapterId).select(EduVideo::getId,EduVideo::getVideoSourceId);
        List<EduVideo> eduVideos = videoMapper.selectList(eduVideoLambdaQueryWrapper);
        return eduVideos;
    }

    @Override
    public void deleteAllByChaperId(String chapterId) {
        /**
         * 根据id删除所有
         */
        LambdaQueryWrapper<EduVideo> eduVideoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduVideoLambdaQueryWrapper.eq(EduVideo::getChapterId, chapterId);
        videoMapper.delete(eduVideoLambdaQueryWrapper);
    }

    @Override
    public void addVideo(VideoVo videoVo) {
        /**
         * 1. 封装
         * 2. 添加
         */
        EduVideo eduVideo = new EduVideo();
        BeanUtils.copyProperties(videoVo, eduVideo);
        videoMapper.insert(eduVideo);

    }

    @Override
    public void updateVideo(VideoVo videoVo) {
        /**
         * 1. 封装
         * 2. 修改
         * 3. 判断
         */
        EduVideo eduVideo = new EduVideo();
        BeanUtils.copyProperties(videoVo, eduVideo);
        int update = videoMapper.updateById(eduVideo);
        if (update == 0) {
            throw new DqException(ResultCode.ERROR.getCode(), "修改失败");
        }
    }

    @Override
    public void deleteVideoById(String videoId) {
        /**
         * 删除单个视频
         * 0. Feign
         * 1. 拿到小节id 最终获取视频id
         * 2. 删除视频  TODO 根据小节id得到视频id
         * 3. 先删除视频，再删除小节
         */
        LambdaQueryWrapper<EduVideo> eduVideoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduVideoLambdaQueryWrapper.eq(EduVideo::getId, videoId).select(EduVideo::getVideoSourceId);
        EduVideo eduVideo = videoMapper.selectOne(eduVideoLambdaQueryWrapper);
        String videoSourceId = eduVideo.getVideoSourceId();
        //根据视频id远程调用
        if (!StringUtils.isEmpty(videoSourceId)) {
            Result result = vodClient.deleteAliyunViedo(videoSourceId);
            if (result.getCode() == 20001){
                //如果失败了 会自动执行熔断降级操作
                throw new DqException(20001,"删除视频失败，熔断器");
            }
        }
        videoMapper.deleteById(videoId);
    }

    @Override
    public VideoVo getVideoById(String videoId) {
        /**
         * 1. 获取当前的小节id
         * 2. 进行查询
         * 3. 结果封装
         */
        LambdaQueryWrapper<EduVideo> eduVideoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduVideoLambdaQueryWrapper.eq(EduVideo::getId, videoId).select(EduVideo::getId, EduVideo::getTitle, EduVideo::getIsFree, EduVideo::getIsFree, EduVideo::getVideoOriginalName, EduVideo::getVideoSourceId).last("limit 1");
        EduVideo eduVideo = videoMapper.selectOne(eduVideoLambdaQueryWrapper);
        VideoVo videoVo = new VideoVo();
        BeanUtils.copyProperties(eduVideo, videoVo);
        return videoVo;
    }

    @Override
    public void deleteVideoByCourseId(String courseId) {
        /**
         * 删除多个视频
         * 1. 根据课程id查询出所有的视频id
         * 2. TODO 删除视频  获取所有的视频号变为一个ArrayList
         * 3. 删除小节
         * 4. 微服务调用vod中的删除视频方法
         */

        //查询所有courseId的小节id和小节视频id
        LambdaQueryWrapper<EduVideo> eduVideoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduVideoLambdaQueryWrapper.eq(EduVideo::getCourseId, courseId).select(EduVideo::getId,EduVideo::getVideoSourceId);
        List<EduVideo> eduVideos = videoMapper.selectList(eduVideoLambdaQueryWrapper);
        List<String> videoArrayList = new ArrayList<>();
        for (EduVideo eduVideo : eduVideos) {
            String videoSourceId = eduVideo.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)){
                videoArrayList.add(videoSourceId);
            }
        }
        //把小节视频id变为一个集合传入
        if (!videoArrayList.isEmpty()){
            vodClient.deleteAliyunVideoBatch(videoArrayList);
        }

        //删除所有小节
        LambdaQueryWrapper<EduVideo> videoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        videoLambdaQueryWrapper.eq(EduVideo::getCourseId,courseId);
        videoMapper.delete(videoLambdaQueryWrapper);

    }


}